package com.oepm.web.controller;

import com.oepm.biz.UserInfoBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.DictionaryPo;
import com.oepm.dao.entity.UserInfoPo;
import com.oepm.dao.entity.vo.UserPassVo;
import com.oepm.common.utils.RoleUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@Api(tags = "用户基本信息API")
@RequestMapping("/personal")
public class UserInfoController {
    @Autowired
    UserInfoBiz userInfoBiz;

    /**
     * 查询用户所有信息
     * @param request
     * @return
     */
    @GetMapping("/select")
    @ApiOperation("查询用户信息")
    public Result queryById(HttpServletRequest request){
        String token=request.getHeader("Authorization");
        String userId = JWTUtil.getUserId(token);
        UserInfoPo userInfoPo = userInfoBiz.queryById(userId);
        String role = RoleUtil.idToRole(JWTUtil.getRoleId(token));
        userInfoPo.setRole(role);
        Result result;
        if (userInfoPo!=null){
            result = new Result(Meta.SUCCESS);
            result.setData(userInfoPo);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    @PostMapping("/update")
    @ApiOperation("修改/添加用户基本信息")
    public Result addInfo(@RequestBody DictionaryPo dp, ServletRequest request){
        //获取请求头token
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        //获取角色id
        String userId = JWTUtil.getUserId(token);
        //获取基本信息表用户id,判断是跟新还是插入
        String id = userInfoBiz.queryUserId(userId);
        dp.setUserId(userId);

        int i = userInfoBiz.updateInfo(dp);

        if(i==1){
            return new Result(Meta.SUCCESS);
        }

        return new Result(Meta.FAIL);
    }

    /**
     * 查询密码
     * @param request
     * @param passVo
     * @return
     */
    @PostMapping("/verifyPd")
    public Result selectPass(HttpServletRequest request,@RequestBody UserPassVo passVo){
        String token=request.getHeader("Authorization");
        String userId = JWTUtil.getUserId(token);
        String pass = userInfoBiz.selectPass(userId);
        Result result;
        if (pass.equals(passVo.getPassword())){
            System.out.println(pass);
            result = new Result(Meta.SUCCESS);
        }else {
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 更新密码
     * @param request
     * @param passVo
     * @return
     */
    @PostMapping("/updatePd")
    public Result updatePass(HttpServletRequest request,@RequestBody UserPassVo passVo){
        String token=request.getHeader("Authorization");
        String userId = JWTUtil.getUserId(token);
        passVo.setUserId(userId);
        int flag = userInfoBiz.updatePass(passVo);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else {
            result = new Result(Meta.FAIL);
        }
        return result;
    }
}
